/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Telas;

import Classes.Funcionario;
import Controles.AdministradorDAO;
import Controles.Tabela;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author dayan
 */
public class Funcionarios extends javax.swing.JFrame {

    
     Tabela tmFuncionarios = new Tabela(null, new String[]{"ID", "Nome", "CPF", "Login"});
     List<Funcionario> funcionarios;
     ListSelectionModel lsmFuncionarios;
     String tipoCadastro;
    
    /**
     * Creates new form Funcionarios
     */
    public Funcionarios() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tfPesquisa = new javax.swing.JTextField();
        btPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbFuncionarios = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tfCpf = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        tfDataNascimento = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        tfEndereco = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfCidade = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tfUf = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tfTelefone = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        tfLogin = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        pfSenha = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        pfConfirmaSenha = new javax.swing.JPasswordField();
        jLabel11 = new javax.swing.JLabel();
        btNovo = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btFechar = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        tfCep = new javax.swing.JFormattedTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuOpcoes = new javax.swing.JMenu();
        menuSobre = new javax.swing.JMenuItem();
        menuSair = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Funcionários");

        btPesquisar.setText("Pesquisar");
        btPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarActionPerformed(evt);
            }
        });

        tbFuncionarios.setModel(tmFuncionarios);
        tbFuncionarios.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmFuncionarios = tbFuncionarios.getSelectionModel();
        lsmFuncionarios.addListSelectionListener(new ListSelectionListener(){
            public void valueChanged(ListSelectionEvent e){
                if(! e.getValueIsAdjusting()){
                    tbFuncionariosLinhaSelecionada(tbFuncionarios);
                }
            }
        });
        tbFuncionarios.getColumnModel().getColumn(0).setPreferredWidth(50);
        tbFuncionarios.getColumnModel().getColumn(1).setPreferredWidth(265);
        tbFuncionarios.getColumnModel().getColumn(2).setPreferredWidth(120);
        tbFuncionarios.getColumnModel().getColumn(3).setPreferredWidth(150);
        tbFuncionarios.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

        jScrollPane1.setViewportView(tbFuncionarios);

        jLabel1.setText("Nome: *");

        tfNome.setEditable(false);

        jLabel2.setText("CPF: *");

        tfCpf.setEditable(false);
        try {
            tfCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel3.setText("Dt. Nasc.: *");

        tfDataNascimento.setEditable(false);
        try {
            tfDataNascimento.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##-##-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        tfDataNascimento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfDataNascimentoActionPerformed(evt);
            }
        });

        jLabel4.setText("Endereço:");

        tfEndereco.setEditable(false);

        jLabel5.setText("Cidade:");

        tfCidade.setEditable(false);

        jLabel6.setText("UF:");

        tfUf.setEditable(false);

        jLabel7.setText("Telefone:");

        tfTelefone.setEditable(false);
        try {
            tfTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel8.setText("Login: *");

        tfLogin.setEditable(false);
        tfLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfLoginActionPerformed(evt);
            }
        });

        jLabel9.setText("Senha: *");

        pfSenha.setEditable(false);

        jLabel10.setText("Confirmar Senha: *");

        pfConfirmaSenha.setEditable(false);

        jLabel11.setText("*: Campos com preenchimento obrigatório");

        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btSalvar.setText("Salvar");
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        btExcluir.setText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.setEnabled(false);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btFechar.setText("Fechar");
        btFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btFecharActionPerformed(evt);
            }
        });

        jLabel12.setText("CEP:");

        tfCep.setEditable(false);
        try {
            tfCep.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        menuOpcoes.setText("Opções");

        menuSobre.setText("Sobre");
        menuSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSobreActionPerformed(evt);
            }
        });
        menuOpcoes.add(menuSobre);

        menuSair.setText("Sair");
        menuSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuSairActionPerformed(evt);
            }
        });
        menuOpcoes.add(menuSair);

        jMenuBar1.add(menuOpcoes);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tfPesquisa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btPesquisar))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel4))
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(tfNome)
                                    .addComponent(tfEndereco))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(41, 41, 41)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel12)
                                    .addComponent(tfCep, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(tfCidade, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(tfDataNascimento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                                    .addComponent(jLabel5))
                                .addGap(33, 33, 33)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfCpf, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel6)
                                    .addComponent(tfUf, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(tfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(346, 346, 346)
                                .addComponent(jLabel10))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(btNovo)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btSalvar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(btAlterar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(btExcluir)
                                    .addGap(18, 18, 18)
                                    .addComponent(btCancelar)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(btFechar))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(tfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel8))
                                    .addGap(27, 27, 27)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel9)
                                        .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(29, 29, 29)
                                    .addComponent(pfConfirmaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 14, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btAlterar, btCancelar, btExcluir, btFechar, btNovo, btSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfPesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCpf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfDataNascimento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfUf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfCep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pfSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(pfConfirmaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tfLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNovo)
                    .addComponent(btSalvar)
                    .addComponent(btAlterar)
                    .addComponent(btExcluir)
                    .addComponent(btCancelar)
                    .addComponent(btFechar))
                .addGap(18, 18, 18)
                .addComponent(jLabel11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        tipoCadastro = "novo";
        novoFuncionario();
    }//GEN-LAST:event_btNovoActionPerformed

    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if(tipoCadastro.equals("novo")){
            cadastraFuncionario();
        } else if(tipoCadastro.equals("alterar")){
            modificaFuncionario();
            listarFuncionarios();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
         tipoCadastro = "alterar";
         alteraFuncionario();
    }//GEN-LAST:event_btAlterarActionPerformed

    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
         try {
             excluir();
         } catch (SQLException ex) {
             Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
         }
         listarFuncionarios();
    }//GEN-LAST:event_btExcluirActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
       if(JOptionPane.showConfirmDialog(null, "Deseja Cancelar esta ação?", "ATENÇÃO", JOptionPane.YES_NO_OPTION)==0){
            desabilitaCampos();
        }
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFecharActionPerformed
       if(JOptionPane.showConfirmDialog(null, "Deseja sair da tela de Funcionários?", "ATENÇÃO!", JOptionPane.YES_NO_OPTION)==0){
           this.dispose();
       }
       desabilitaCampos();
       tbFuncionarios.getSelectionModel().clearSelection();
    }//GEN-LAST:event_btFecharActionPerformed

    private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarActionPerformed
         listarFuncionarios();
    }//GEN-LAST:event_btPesquisarActionPerformed

    private void tfLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfLoginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfLoginActionPerformed

    private void tfDataNascimentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfDataNascimentoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfDataNascimentoActionPerformed

    private void menuSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSobreActionPerformed
        JOptionPane.showMessageDialog(null, "Sistema de Emissão de Carteirinhas v1.0 desenvolvido por: \nDayan Costa\nIury Rezende"
                + "\nLuciano Costa\nVitor Almeida");
    }//GEN-LAST:event_menuSobreActionPerformed

    private void menuSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuSairActionPerformed
        if (JOptionPane.showConfirmDialog(null, "Esta ação irá sair do sistema por completo, confirma esta ação?", "ATENÇÃO!", JOptionPane.YES_NO_OPTION) == 0) {
            System.exit(0);
        }
    }//GEN-LAST:event_menuSairActionPerformed

    public void habilitaCampos(){
      btNovo.setEnabled(false);
      btFechar.setEnabled(false);
      btExcluir.setEnabled(false);
      btAlterar.setEnabled(false);
      btCancelar.setEnabled(true);
      btSalvar.setEnabled(true);
      tfNome.setEditable(true);
      tfDataNascimento.setEditable(true);
      tfCpf.setEditable(true);
      tfTelefone.setEditable(true);
      tfEndereco.setEditable(true);
      tfCidade.setEditable(true);
      tfUf.setEditable(true);
      tfCep.setEditable(true);
      tfLogin.setEditable(true);
      pfSenha.setEditable(true);
      pfConfirmaSenha.setEditable(true);
    }
    public void desabilitaCampos(){
      btNovo.setEnabled(true);
      btFechar.setEnabled(true);
      btExcluir.setEnabled(true);
      btAlterar.setEnabled(true);
      btCancelar.setEnabled(false);
      btSalvar.setEnabled(false);
      tfNome.setEditable(false);
      tfDataNascimento.setEditable(false);
      tfCpf.setEditable(false);
      tfTelefone.setEditable(false);
      tfEndereco.setEditable(false);
      tfCidade.setEditable(false);
      tfUf.setEditable(false);
      tfCep.setEditable(false);
      tfLogin.setEditable(false);
      pfSenha.setEditable(false);
      pfConfirmaSenha.setEditable(false);
    }
    public void novoFuncionario(){
      btExcluir.setEnabled(false);
      btAlterar.setEnabled(false);
      btCancelar.setEnabled(true);
      habilitaCampos();
      tfNome.setText("");
      tfDataNascimento.setText("");
      tfCpf.setText("");
      tfTelefone.setText("");
      tfEndereco.setText("");
      tfCidade.setText("");
      tfUf.setText("");
      tfCep.setText("");
      tfLogin.setText("");
      pfSenha.setText("");
      pfConfirmaSenha.setText("");
    }
    
    public boolean verificaNome(){//verifica se o campo nome esta preenchido
        if(!tfNome.getText().trim().equals("")){ 
            return true;
        }else{
            JOptionPane.showMessageDialog(null, "Campo nome é obrigatório!");
            return false;
        }    
    }
    public boolean verificaCpf(String cpf){ 
        boolean resultado;
        AdministradorDAO dao = new AdministradorDAO();
        
         try {
             funcionarios = dao.listarFuncionariosCpf("%"+tfCpf.getText().trim()+"%");
         } catch (SQLException ex) {
             Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
         }
        if(funcionarios.isEmpty()){
            resultado = true;
        } else{
            JOptionPane.showMessageDialog(null, "Funcionário com este CPF já cadastrado, por favor confira os dados do funcionário!");
            tfCpf.requestFocus();
            resultado = false;
        }
        
        dao = null;
        return resultado;
    }
    public boolean verificaCpf(){
        if(!tfCpf.getText().trim().equals("")){
            return true;
        } else{
            JOptionPane.showMessageDialog(null, "Campo CPF é obrigatório!");
            return false;
        }
    }
    public boolean verificaUf(){
        if(tfUf.getText().length() > 0 && tfUf.getText().length()<2){
            JOptionPane.showMessageDialog(null, "Campo UF deve contar 2 Letras!");
            tfUf.requestFocus();
            return false;
        }else{
            return true;
        }
    }
    public boolean verificaDataNascimento(){
        if(!tfDataNascimento.getText().trim().equals("")){
            return true;
        } else{
            JOptionPane.showMessageDialog(null, "Data inválida!");
            tfDataNascimento.requestFocus();
            return false;
        }
    }
    public boolean verificaLogin(){
        if(!tfLogin.getText().trim().equals("")){
            return true;
        } else{
            JOptionPane.showMessageDialog(null, "Campo de Login de usuário é obrigatório!");
            tfLogin.requestFocus();
            return false;
        }
    }
    public boolean verificaSenha(){
        if(pfSenha.getText().trim().equals(pfConfirmaSenha.getText().trim())){
            return true;
        } else{
            JOptionPane.showMessageDialog(null, "Senhas não conferem, digite novamente!");
            return false;
        }
    }
    public boolean verificaLogin(String login){
        boolean resultado;
        AdministradorDAO dao = new AdministradorDAO();
        
         try {
             funcionarios = dao.listarFuncionariosLogin("%"+tfLogin.getText().trim()+"%");
         } catch (SQLException ex) {
             Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
         }
        if(funcionarios.isEmpty()){
            resultado = true;
        } else {
            JOptionPane.showMessageDialog(null, "Login já cadastrado no sistema, tente outro login!");
            tfLogin.requestFocus();
            resultado = false;
        }
        dao = null;
        return resultado;
    }
    
    public boolean validarDataNascimento(String dataNascimento){
        Date data = null;
        
        String dataTexto = new String(dataNascimento);
        SimpleDateFormat formatoData = new SimpleDateFormat("dd/MM/yyyy");
        try{
            formatoData.setLenient(false);
            data = formatoData.parse(dataTexto);
            return true;
        } catch(ParseException e){
            JOptionPane.showMessageDialog(null, "Data de Nascimento Inválida. Digite uma data Válida!", "Aviso", JOptionPane.WARNING_MESSAGE);
            tfDataNascimento.requestFocus();
            return false;
        }
    }
    
    private void cadastraFuncionario(){
        if(verificaNome() && verificaCpf("%"+tfCpf.getText().trim()+"%") && verificaLogin("%"+tfLogin+"%") && verificaUf() && verificaDataNascimento() && verificaLogin() && verificaSenha() && validarDataNascimento(tfDataNascimento.getText().trim())){
            try {
                Funcionario func = new Funcionario();
                
                func.setNome(tfNome.getText().trim());
                func.setDataNascimento(tfDataNascimento.getText().trim());
                func.setCpf(tfCpf.getText().trim());
                func.setEndereco(tfEndereco.getText().trim());
                func.setCep(tfCep.getText().trim());
                func.setCidade(tfCidade.getText().trim());
                func.setUf(tfUf.getText().trim());
                func.setTelefone(tfTelefone.getText().trim());
                func.setLogin(tfLogin.getText().trim());
                func.setSenha(pfSenha.getText().trim());
                
                AdministradorDAO dao = new AdministradorDAO();
                dao.cadastraFuncionario(func);
                JOptionPane.showMessageDialog(null, func.getNome() + " cadastrado com sucesso!");
                desabilitaCampos();
                dao = null;
                func = null;
            } catch (SQLException ex) {
                Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    private void mostrarFuncionarios(List<Funcionario> funcionarios){
        while(tmFuncionarios.getRowCount()>0){
            tmFuncionarios.removeRow(0);
        }
        if(funcionarios.isEmpty()){
            JOptionPane.showMessageDialog(null, "Nenhum funcionário encontrado!");
        } else {
            String [] linha = new String[]{null, null, null, null};
            for(int i = 0; i<funcionarios.size(); i++){
                tmFuncionarios.addRow(linha);
                tmFuncionarios.setValueAt(funcionarios.get(i).getId(), i, 0);
                tmFuncionarios.setValueAt(funcionarios.get(i).getNome(), i, 1);
                tmFuncionarios.setValueAt(funcionarios.get(i).getCpf(), i, 2);
                tmFuncionarios.setValueAt(funcionarios.get(i).getLogin(), i, 3);
            }
        }
    }
    private void listarFuncionarios(){
        AdministradorDAO dao = new AdministradorDAO();
         try {
             funcionarios = dao.listarFuncionarios("%" + tfPesquisa.getText().trim() + "%");
         } catch (SQLException ex) {
             Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
         }
        mostrarFuncionarios(funcionarios);
        dao = null;
    }
    private void tbFuncionariosLinhaSelecionada(JTable table){
        if(tbFuncionarios.getSelectedRow() != -1){ //retorna -1 quando não há nenhuma linha da tabela selecionada
             tfNome.setText(funcionarios.get(table.getSelectedRow()).getNome());
             tfDataNascimento.setText(funcionarios.get(table.getSelectedRow()).getDataNascimento());
             tfCpf.setText(funcionarios.get(table.getSelectedRow()).getCpf());
             tfEndereco.setText(funcionarios.get(table.getSelectedRow()).getEndereco());
             tfCidade.setText(funcionarios.get(table.getSelectedRow()).getCidade());
             tfUf.setText(funcionarios.get(table.getSelectedRow()).getUf());
             tfCep.setText(funcionarios.get(table.getSelectedRow()).getCep());
             tfTelefone.setText(funcionarios.get(table.getSelectedRow()).getTelefone());
             tfLogin.setText(funcionarios.get(table.getSelectedRow()).getLogin());
             
        }
    }
    private void alteraFuncionario(){        
        if(tbFuncionarios.getSelectedRow()!= -1){
            habilitaCampos();
            pfSenha.setEditable(false);
            pfConfirmaSenha.setEditable(false);
            btExcluir.setEnabled(false);
            btNovo.setEnabled(false);            
        } else{
            JOptionPane.showMessageDialog(this, "Selecione um funcionário na tabela!");
        }
    }
    private void modificaFuncionario(){
        if(verificaNome() && verificaCpf() && verificaUf() && verificaDataNascimento() && verificaLogin() && verificaSenha()){
            try {
                Funcionario func = new Funcionario();
                
                func.setNome(tfNome.getText().trim());
                func.setDataNascimento(tfDataNascimento.getText().trim());
                func.setCpf(tfCpf.getText().trim());
                func.setEndereco(tfEndereco.getText().trim());
                func.setCep(tfCep.getText().trim());
                func.setCidade(tfCidade.getText().trim());
                func.setUf(tfUf.getText().trim());
                func.setTelefone(tfTelefone.getText().trim());
                func.setLogin(tfLogin.getText().trim());
                func.setId(funcionarios.get(tbFuncionarios.getSelectedRow()).getId());
                
                AdministradorDAO dao = new AdministradorDAO();
                dao.alteraFuncionario(func);
                JOptionPane.showMessageDialog(null, func.getNome() + " atualizado com sucesso!");
                desabilitaCampos();
            } catch (SQLException ex) {
                Logger.getLogger(Funcionarios.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    private void excluir() throws SQLException{
        if(JOptionPane.showConfirmDialog(null, "Deseja excluir este funcionário?", "ATENÇÃO", JOptionPane.YES_NO_OPTION)==0){
            if(tbFuncionarios.getSelectedRow()!= -1){  
                AdministradorDAO cli = new AdministradorDAO();
                cli.excluirFuncionario(funcionarios.get(tbFuncionarios.getSelectedRow()));
                JOptionPane.showMessageDialog(null, "Funcionário excluido com sucesso!");
                listarFuncionarios();
            } else {
                JOptionPane.showMessageDialog(null, "Selecione um Funcionário na tabela!");
            }
        }
    }
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Funcionarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Funcionarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Funcionarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Funcionarios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Funcionarios().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btFechar;
    private javax.swing.JButton btNovo;
    private javax.swing.JButton btPesquisar;
    private javax.swing.JButton btSalvar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenu menuOpcoes;
    private javax.swing.JMenuItem menuSair;
    private javax.swing.JMenuItem menuSobre;
    public javax.swing.JPasswordField pfConfirmaSenha;
    public javax.swing.JPasswordField pfSenha;
    private javax.swing.JTable tbFuncionarios;
    private javax.swing.JFormattedTextField tfCep;
    private javax.swing.JTextField tfCidade;
    private javax.swing.JFormattedTextField tfCpf;
    private javax.swing.JFormattedTextField tfDataNascimento;
    private javax.swing.JTextField tfEndereco;
    public javax.swing.JTextField tfLogin;
    private javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfPesquisa;
    private javax.swing.JFormattedTextField tfTelefone;
    private javax.swing.JTextField tfUf;
    // End of variables declaration//GEN-END:variables
}
